package com.game.majiang.vo;

import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;

import javax.sql.rowset.CachedRowSet;

import com.game.majiang.config.DataManager;
import com.game.majiang.config.LogManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.Parameter;
import com.game.server.db.ParameterType;
import com.game.server.db.SqlHelper;
import com.game.server.db.SqlHelperwrite;
import com.game.server.message.proto.out.TabOutintegralChangeRsp;
import com.game.server.message.proto.out.UserInfo;

/**
 * @author
 *
 */

public class UserVo extends UserDBVo{

	public UserVo(int id) {
		super(id);
	}

	public UserVo(int id,RobotVo rv) {
		super(id,rv);
	}

	public UserVo() {
		super();
	}

	// public UserVo getUserByUserId(Integer id) {
	//
	// }

	/**
	 * 增加金钱
	 * @param add
	 * @return
	 */
	public int AddGold(int add) {
		this.setGold(this.getGold()+add);
		if(this.getGold()<0){
			this.setGold(0);
		}
		return this.getGold();
	}

	//设置权限
	public int SetTest(int add) {
		this.setGold(add);
		if(this.getGold()<0){
			this.setGold(0);
		}
		return this.getGold();
	}

	//增加钻石
	public int AddDiamonds(int add) {
		//		this.setCard(this.getCard()+add);
		this.setDiamonds(this.getDiamonds()+add);
		if(this.getDiamonds()<0){
			this.setDiamonds(0);
		}
		return this.getDiamonds();
	}

	//增加积分
	public int AddIntegral(int add) {

		System.out.println("增加积分"+add);
		this.setIntegral(this.getIntegral()+add);
		if(this.getIntegral()<0){
			this.setIntegral(0);
		}
		return this.getIntegral();
	}

	/**
	 * 结果统计
	 * @return
	 */
	public void resultStatistics(boolean winBool, boolean groupCard) {
		this.setZongshoucount(this.getZongshoucount() + 1);
		if(winBool) {
			this.setZongshouwincount(this.getZongshouwincount() + 1);
		}
		if(groupCard) {
			this.setZupaicount(this.getZupaicount() + 1);
			if(winBool) {
				this.setZupaiwincount(this.getZupaiwincount() + 1);
			}
		}
	}

	public void addZongjuCount() {
		this.setZongjucount(this.getZongjucount() + 1);
	}

	public void addZongwinjucount() {
		this.setZongwinjucount(this.getZongwinjucount() + 1);
	}

	public void addZongLosejuCount() {
		this.setZonglosejucount(this.getZonglosejucount() + 1);
	}

	public static void updateZongwinjuCount(int id) {
		String cmdText = "update users set zongwinjucount = zongwinjucount+1 where id=?";
		Parameter p1 = new Parameter(Types.INTEGER, id, ParameterType.Input);
		SqlHelper.execute(cmdText,p1);
	}

	public static void updateZongLosejuCount(int id) {
		String cmdText = "update users set zonglosejucount = zonglosejucount+1 where id=?";
		Parameter p1 = new Parameter(Types.INTEGER, id, ParameterType.Input);
		SqlHelper.execute(cmdText,p1);
	}

	public String GetHeadUrl( ) {
		//		if(getHeadType() == 0)
		//		{
		//			return  DataManager.getInstance().systemValue("imgPath") + getHead();
		//		}
		//		else
		//		{
		return getHead();
		//		}
	}

	//添加gps 最近登录
	public void addListWarpAndWefts(String str) {
		this.getListWarpAndWefts().add(0,str);

	}

	static public int CheckPassword(String username,String password) {//and status <> 0
		String cmdText = "select id from users where Account=? and Password=? ";
		Parameter p1 = new Parameter(Types.NVARCHAR, username, ParameterType.Input);
		Parameter p2 = new Parameter(Types.NVARCHAR, password, ParameterType.Input);
		Object obj = SqlHelperwrite.execScalar(cmdText, p1,p2);
		if(obj == null)
			return -1;
		else
			return (Integer)obj;
	}

	public static int CheckAccount(String accout) {
		String cmdText = "select id from users  where account = ?";
		Parameter p1 = new Parameter(Types.VARCHAR, accout, ParameterType.Input);
		Object obj = SqlHelperwrite.execScalar(cmdText,p1);
		if(obj == null)
			return -1;
		else
			return (Integer)obj;
	}

	public static boolean checkNickname(String nickname,int id) {
		String cmdText = "select id from users  where nickname = ? and id!=?";
		Parameter p1 = new Parameter(Types.VARCHAR, nickname, ParameterType.Input);
		Parameter p2 = new Parameter(Types.INTEGER, id, ParameterType.Input);
		Object obj = SqlHelper.execScalar(cmdText,p1,p2);
		if(obj == null)
			return false;
		else
			return true;
	}

	public static int checkNameupdate(int id) {
		String cmdText = "select nameupdate from users  where id=?";
		Parameter p1 = new Parameter(Types.INTEGER, id, ParameterType.Input);
		Object obj = SqlHelper.execScalar(cmdText,p1);
		return obj!=null? (int) Double.parseDouble(obj.toString()) : 0;
	}


	public static void updateNameupdateMinus(int id) {
		String cmdText = "update users set nameupdate = nameupdate-1 where id=?";
		Parameter p1 = new Parameter(Types.INTEGER, id, ParameterType.Input);
		SqlHelper.execute(cmdText,p1);
	}


	/**
	 * 充值
	 * @return
	 */
	public  ArrayList<Integer> getRecharge() {
		ArrayList<Integer> list = new ArrayList<Integer>();
		String cmdText = "select gold from recharge where userId = ? and status=0";
		Parameter p1 = new Parameter(Types.INTEGER, this.getId(), ParameterType.Input);
		CachedRowSet rs = SqlHelper.getResultSet(cmdText, p1);
		try {
			while (rs.next()) {
				list.add(rs.getInt(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();
			LogManager.getInstance().Error("Sql:" + cmdText + "错误!");
		}
		return list;
	}


	/**
	 * 更新充值记录ֵ״
	 * @param id
	 */
	public static void updateRecharge(int id) {
		String cmdText = "update recharge set status = 1 where userId=? and status = 0";
		Parameter p1 = new Parameter(Types.INTEGER, id, ParameterType.Input);
		SqlHelperwrite.execute(cmdText, p1);
	}


	public static int checkToken(String account, String token) {
		String cmdText = "select id from users where account=?  and status <> 0";
		Parameter p1 = new Parameter(Types.NVARCHAR, account, ParameterType.Input);

		Object obj = SqlHelperwrite.execScalar(cmdText, p1);
		if(obj == null)
			return -1;
		else
			return (Integer)obj;
	}


	public static void updateGlod(int userid, int gold,int type) {
		String cmdText = "";
		String biao="";
//		if(userid<10000){
//			biao="robot";
//		}else{
//			biao="users";
//		}
		biao="users";

		if(type==1){
			cmdText = "update "+biao+" set Gold = Gold +? where Id=?";
		} else if(type==2) {
			cmdText = "update "+biao+" set diamonds = diamonds +? where Id=?";
		} else if(type ==3) {
			cmdText = "update "+biao+" set integral = integral +? where Id=?";
		}
		if("".equals(cmdText)){

			return;
		}

		Parameter p2 = new Parameter(Types.INTEGER, gold, ParameterType.Input);
		Parameter p3 = new Parameter(Types.INTEGER, userid, ParameterType.Input);
		SqlHelperwrite.execute(cmdText, p2,p3);
	}

	public static int getGlodm(int userid,int type ) {
		String cmdText = "";
		if(type ==1) {
			cmdText = "select gold from users where id=?";
		} else if(type ==2) {
			cmdText = "select diamonds from users where id=?";
		} else if(type ==3) {
			cmdText = "select integral from users where id=?";
		}
		Parameter p1 = new Parameter(Types.INTEGER, userid, ParameterType.Input);
		Object obj = SqlHelper.execScalar(cmdText, p1);
		if(obj!=null){
			return (int)obj;
		}
		return 0;
	}

	public int getRoomId() {
		String cmdText = "select id from room where agentId=? ";
		Parameter p1 = new Parameter(Types.INTEGER, this.getId(), ParameterType.Input);
		Object obj = SqlHelper.execScalar(cmdText, p1);
		if(obj == null)
			return -1;
		else
			return (int)obj;

	}


	/**
	 *查询用户是否绑代理
	 */
	public static int bindingProxy(int uid ) {

		String sql = "select pid from users where id=? and status <>0";
		Parameter p1 = new Parameter(Types.INTEGER, uid, ParameterType.Input);

		Object obj= SqlHelper.execScalar(sql,p1);
		if(obj == null)
			return -1;
		else
			return (int)obj;

	}

	/**
	 *用户绑代理
	 */
	public static void bindingProxyId(int pid,int uid ) {

		String sql = "update users set pid= ? where id = ?";
		Parameter p1 = new Parameter(Types.INTEGER, pid, ParameterType.Input);
		Parameter p2 = new Parameter(Types.INTEGER, uid, ParameterType.Input);
		SqlHelperwrite.execute(sql,p1,p2);

	}

	/**
	 * 添加推荐人
	 */
	public static void addtuijian(int tuijian,int id ) {


		String sql = "update users set pid= ? where id = ?";
		Parameter p1 = new Parameter(Types.INTEGER, tuijian, ParameterType.Input);
		Parameter p2 = new Parameter(Types.INTEGER, id, ParameterType.Input);

		SqlHelperwrite.execute(sql,p1,p2);


	}

	/**
	 * 修改用户信息
	 */
	public static boolean updateUser(int id , byte sex,String qianming, String nickname, String head) {


		String sql = "update users set sex = ?,autograph=?, nickname = ?, head = ?  where id=?";
		Parameter p1 = new Parameter(Types.TINYINT, sex, ParameterType.Input);
		Parameter p2 = new Parameter(Types.VARCHAR, qianming, ParameterType.Input);
		Parameter p3 = new Parameter(Types.VARCHAR, nickname, ParameterType.Input);
		Parameter p4 = new Parameter(Types.VARCHAR, head, ParameterType.Input);
		Parameter p5 = new Parameter(Types.INTEGER, id, ParameterType.Input);
		int i= SqlHelperwrite.execute(sql,p1,p2,p3,p4,p5);
		return i>0;

	}

	public static void getuserinfo(LogicClient client) {
		client.setLost(false);
		UserInfo rsp = new UserInfo();
		UserVo vo = client.getVo();
		rsp.setId(client.getUserId());
		rsp.setNickName(vo.getNickName());
		rsp.setGold(vo.getGold());
		rsp.setHead(vo.GetHeadUrl());
		rsp.setSex(vo.getSex());
		rsp.setDiamonds(vo.getDiamonds());
		rsp.setAutograph(vo.getAutograph());
		rsp.setIntegral(vo.getIntegral());
		rsp.setNameupdate(vo.getNameupdate());
		LogManager.getInstance().Log("++++++++++++++++++");
		LogManager.getInstance().Log("setUpdateNameIntegral:"+DataManager.getInstance().getSys_parameter().get("informationupdateconsumption"));
		rsp.setUpdateNameIntegral(Integer.parseInt(DataManager.getInstance().getSys_parameter().get("informationupdateconsumption")));
		rsp.setDownloadurl(DataManager.getInstance().getSys_parameter().get("AndroidDownloadLink"));
		rsp.setKefuwx(DataManager.getInstance().getSys_parameter().get("kefuwx"));
		rsp.setServiceFeeProportion(DataManager.getInstance().systemValue("setbobotip"));
		client.Send(rsp);
		client.setLost(false);

	}

	/**
	 * 通知在桌子内的的用户积分发生变动
	 * @param client
	 * @param type 1 积分 2信誉分
	 * @param val
	 */
	public static void  NoticeTabUserXinyuChange(LogicClient client,int type,int val){
		TabOutintegralChangeRsp changeRsp = new TabOutintegralChangeRsp();
		changeRsp.setType(type);
		changeRsp.setVal(val);
		client.Send(changeRsp);
	}

	public static void insertUserRanking(int uid,String countent) {
		String sql = "INSERT INTO userranking (id,content) VALUES (?,'')";
		Parameter p1 = new Parameter(Types.INTEGER, uid, ParameterType.Input);
		SqlHelperwrite.execute(sql, p1);
	}


}
